package com.ops.uitl.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import com.ops.uitl.common.Common;
import com.ops.uitl.data.DataMap;
import com.ops.uitl.data.IData;
import com.ops.uitl.data.IDataset;

public class PropertiesConfig implements IConfig{
		
		private Properties props;
		private String filepath;
		
		/**
		 * construct function
		 * @param in
		 * @throws Exception
		 */
		public PropertiesConfig(InputStream in) throws Exception {
			props = new Properties();
			props.load(in);
		}
		
		/**
		 * construct function
		 * @param file
		 * @throws Exception
		 */
		public PropertiesConfig(String file) throws Exception {
			this(Common.getInstance().getClassResourceStream(file));
		}
		
		/**
		 * construct function
		 * @param file
		 * @throws Exception
		 */
		public PropertiesConfig(File file) throws Exception {
			this(new FileInputStream(file));
			filepath = file.getPath();
		}
		
		/**
		 * get property
		 * @param prop
		 * @throws Exception
		 */
		public String getProperty(String prop) throws Exception {
			return props.getProperty(prop);
		}
		
		/**
		 * get properties
		 * @return IData
		 * @throws Exception
		 */
		public IData getProperties() throws Exception {
			IData data = new DataMap();
			
			Enumeration e = props.keys();
			while (e.hasMoreElements()) {
				String key = (String) e.nextElement();
				data.put(key, props.getProperty(key));
			}
			
			return data;
		}
		
		/**
		 * get properties
		 * @param prop
		 * @return IData
		 * @throws Exception
		 */
		public IData getProperties(String prop) throws Exception {
			return getProperties();
		}
		
		/**
		 * set property
		 * @param prop
		 * @param value
		 * @throws Exception
		 */
		public void setProperty(String prop, String value) throws Exception {
			props.setProperty(prop, value);
		}
		
		/**
		 * save
		 * @throws Exception
		 */
		public void save() throws Exception {
			FileOutputStream out = new FileOutputStream(filepath);   
			props.store(out, null);
			out.close();
		}

		/**
		 * get elements
		 * @param path
		 * @return IDataset
		 * @throws Exception
		 */
		public IDataset getElements(String path) throws Exception {
			return null;
		}
}
